import 'package:check_app/dao/select_dao.dart';
import 'package:check_app/widgets/AzListWidget.dart';
import 'package:check_app/widgets/SearchBar.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

const CUSTOMER_PATH = '/customer/list';
const BOOK_PATH = '/guideBook/list';
const SUBJECT_PATH = '/subjectClass/subClass/list';

class SelectPage extends StatefulWidget {
  final String type;
  final String title;
  final String placeholder;
  final String label;
  final String value;
  const SelectPage({
    Key key,
    this.type,
    this.title,
    this.placeholder,
    this.label,
    this.value,
  }) : super(key: key);
  @override
  State<StatefulWidget> createState() => new _SelectPageState();
}

class _SelectPageState extends State<SelectPage> {
  bool _loading = false;
  String text;
  List selectList = [];

  @override
  void initState() {
    super.initState();
    if (widget.type == 'customer') {
      getData(CUSTOMER_PATH);
    } else if (widget.type == 'book') {
      getData(BOOK_PATH);
    } else {
      getData(SUBJECT_PATH);
    }
  }

  Future<Null> getData(String path) async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await SelectDao.fetch(path, {
        'name': text,
        'pageSize': 100,
      });
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          selectList = result.data;
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(widget.title),
        // actions: <Widget>[Container()],
      ),
      body: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(bottom: 11),
            decoration: BoxDecoration(
              color: Color.fromRGBO(255, 255, 255, 1),
              border: Border(
                bottom: BorderSide(
                  width: 0.5,
                  color: Color.fromRGBO(229, 229, 229, 1),
                ),
              ),
            ),
            child: SearchBar(
              placeHolder: widget.placeholder,
              searchCb: (value) {
                setState(() {
                  text = value;
                  selectList = [];
                });
                print(text);
                if (widget.type == 'customer') {
                  getData(CUSTOMER_PATH);
                } else {
                  getData(BOOK_PATH);
                }
              },
            ),
          ),
          Expanded(
            child: selectList.length > 0
                ? AzListWidget(
                    list: selectList,
                    label: widget.label,
                    value: widget.value,
                  )
                : SizedBox(),
          ),
        ],
      ),
    );
  }
}
